﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XQ.RequestOfficeSupplies.DAL.DAL
{
    /// <summary>
    /// 角色数据操作类
    /// </summary>
    public class RoleDal:BaseDal<Sys_Roles>
    {
        /// <summary>
        /// 根据节点ID获取角色列表
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public List<Sys_Roles> GetRolesByNodeId(int nodeId)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                string sql = "select r.* from Sys_Roles r inner join FlowNode_Role fnr on r.ID=fnr.RoleID where fnr.FlowNodeID=@FlowNodeID";
                object[] par = { 
                            new SqlParameter("@FlowNodeID",nodeId),
                            };
                return ce.Database.SqlQuery<Sys_Roles>(sql, par).ToList();
            }
        }

        /// <summary>
        /// 根据用户ID获取角色
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Sys_Roles GetRoleByUserId(int userId)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                string sql = "select r.* from Sys_Roles r inner join Sys_User_Roles ur on r.ID=ur.RoleID where ur.UserID=@userId";
                object[] par = { 
                            new SqlParameter("@userId",userId),
                            };
                return ce.Database.SqlQuery<Sys_Roles>(sql, par).FirstOrDefault();
            }
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns></returns>
        public bool DeleteByIds(List<int> ids)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                if (ids != null && ids.Count > 0)
                {
                    foreach (int id in ids)
                    {
                        Sys_Roles role = ce.Sys_Roles.FirstOrDefault(r => r.ID == id);
                        if (role != null)
                        {
                            ce.Sys_Roles.Remove(role);
                        }
                    }
                    return ce.SaveChanges() > 0;
                }
                return false;
            }
        }

        /// <summary>
        /// 是否为部门权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool IsDepartmentRole(int roleId)
        {
            using (RequestOfficeSuppliesEntities ce = new RequestOfficeSuppliesEntities())
            {
                Sys_Roles role = ce.Sys_Roles.FirstOrDefault(r => r.ID == roleId);
                if (role != null && role.isDepartmentRole == true)
                {
                    return true;
                }
                return false;
            }
        }
    }
}
